package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.AskAnswerSiteDTO;
import com.api.shopapi.entity.dto.AskAnswerSiteItemDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 站点发起的问卷调查
 * */
@RestController
@RequestMapping("AskAnswerRecordSite")
public class AskAnswerRecordSiteController extends BaseController{


    @Autowired
    @Qualifier("AskAnswerRecordSiteItemServiceImplOne")
    private AskAnswerRecordSiteItemService askAnswerRecordSiteItemService;


    @Autowired
    @Qualifier("AskAnswerRecordSiteServiceImplOne")
    private AskAnswerRecordSiteService askAnswerRecordSiteService;


    @Autowired
    @Qualifier("AskAnswerRecordServiceImplOne")
    private AskAnswerRecordService askAnswerRecordService;


    @Autowired
    @Qualifier("AskAnswerGroupServiceImplOne")
    private AskAnswerGroupService askAnswerGroupService;

    @Autowired
    @Qualifier("AskAnswerItemServiceImplOne")
    private AskAnswerItemService askAnswerItemService;

    @Autowired
    @Qualifier("AskAnswerNameServiceImplOne")
    private AskAnswerNameService askAnswerNameService;


    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;


    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String askAnswerRecordCode = jsonObject.getString("askAnswerRecordCode");

            JSONArray jsonArray = jsonObject.getJSONArray("arr");


            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AskAnswerRecord/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            int sort = 1;

            List<AskAnswerRecordSite> list = askAnswerRecordSiteService.listByAskAnswerRecordCode(askAnswerRecordCode);
            if(list.size()>0){
                sort = list.get(0).getSort()+1;
            }


            AskAnswerRecordSite askAnswerRecordSite = new AskAnswerRecordSite();
            askAnswerRecordSite.setAskAnswerRecordSiteCode(UUID.randomUUID().toString());
            askAnswerRecordSite.setAskAnswerRecordCode(askAnswerRecordCode);
            askAnswerRecordSite.setSort(sort);
            askAnswerRecordSite.setCreateTime(new Date());
            askAnswerRecordSiteService.insert(askAnswerRecordSite);

            for(int i=0;i<jsonArray.size();i++){

                String askAnswerGroupCode = jsonArray.getJSONObject(i).getString("groupCode");
                String askAnswerItemCode = jsonArray.getJSONObject(i).getString("itemCode");

                AskAnswerGroup askAnswerGroup = askAnswerGroupService.getByAskAnswerGroupCode(askAnswerGroupCode);

                AskAnswerRecordSiteItem askAnswerRecordSiteItem = new AskAnswerRecordSiteItem();
                askAnswerRecordSiteItem.setAskAnswerRecordSiteItemCode(UUID.randomUUID().toString());
                askAnswerRecordSiteItem.setAskAnswerRecordSiteCode(askAnswerRecordSite.getAskAnswerRecordSiteCode());

                askAnswerRecordSiteItem.setAskAnswerNameCode(askAnswerGroup.getAskAnswerNameCode());
                askAnswerRecordSiteItem.setAskAnswerGroupCode(askAnswerGroupCode);
                askAnswerRecordSiteItem.setAskAnswerItemCode(askAnswerItemCode);
                askAnswerRecordSiteItemService.insert(askAnswerRecordSiteItem);
            }


            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerRecord/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //一次调查中调查人数的列表
    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            String askAnswerRecordCode = jsonObject.getString("askAnswerRecordCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/AskAnswerRecord/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<AskAnswerRecordSite> pageInfo = askAnswerRecordSiteService.pageByAskAnswerRecordCode(pageIndex,pageSize, askAnswerRecordCode);

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfo",pageInfo);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerRecord/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //查看调查一人的结果
    @RequestMapping(value = "Get", method = RequestMethod.POST)
    public MyResult Get(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String askAnswerRecordSiteCode = jsonObject.getString("askAnswerRecordSiteCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AskAnswerRecord/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            AskAnswerRecordSite askAnswerSite = askAnswerRecordSiteService.getByAskAnswerRecordSiteCode(askAnswerRecordSiteCode);
            AskAnswerSiteDTO askAnswerSiteDTO = mapper.map(askAnswerSite,AskAnswerSiteDTO.class);

            List<AskAnswerRecordSiteItem> listSiteItem = askAnswerRecordSiteItemService.listByAskAnswerRecordSiteCode(askAnswerRecordSiteCode);
            List<AskAnswerSiteItemDTO> listDTO = new ArrayList<>();
            for(AskAnswerRecordSiteItem askAnswerSiteItem:listSiteItem){

                AskAnswerGroup askAnswerGroup = askAnswerGroupService.getByAskAnswerGroupCode(askAnswerSiteItem.getAskAnswerGroupCode());
                AskAnswerItem askAnswerItem = askAnswerItemService.getByAskAnswerItemCode(askAnswerSiteItem.getAskAnswerItemCode());

                AskAnswerSiteItemDTO askAnswerSiteItemDTO = mapper.map(askAnswerItem,AskAnswerSiteItemDTO.class);
                askAnswerSiteItemDTO.setAskAnswerGroupName(askAnswerGroup.getName());
                askAnswerSiteItemDTO.setAskAnswerItemName(askAnswerItem.getName());
                listDTO.add(askAnswerSiteItemDTO);
            }

            askAnswerSiteDTO.setListAskAnswerSiteItemDTO(listDTO);



            JSONObject jsonObject1 =new JSONObject();
            jsonObject1.put("askAnswerSiteDTO",askAnswerSiteDTO);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerRecord/Add",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //统计一次的调查
    @RequestMapping(value = "Total",method = RequestMethod.POST)
    public MyResult Total(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String askAnswerRecordCode = jsonObject.getString("askAnswerRecordCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/AskAnswerSite/Total");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            List<AskAnswerRecordSite> list = askAnswerRecordSiteService.listByAskAnswerRecordCode(askAnswerRecordCode);

            JSONArray arr = new JSONArray();

            //1:get name
            //2:get group
            //3:get item
            //4:get count



            for(AskAnswerRecordSite askAnswerSite:list){


                List<AskAnswerRecordSiteItem> listSiteItem = askAnswerRecordSiteItemService.listByAskAnswerRecordSiteCode(askAnswerSite.getAskAnswerRecordSiteCode());

                //1
                for(AskAnswerRecordSiteItem askAnswerSiteItem:listSiteItem){
                    String askAnswerNameCode = askAnswerSiteItem.getAskAnswerNameCode();
                    AskAnswerName askAnswerType = askAnswerNameService.getByAskAnswerNameCode(askAnswerNameCode);

                    if(!hasName(arr,askAnswerNameCode)){
                        JSONObject jsonObject1 = new JSONObject();
                        jsonObject1.put("key","type");
                        jsonObject1.put("askAnswerNameCode",askAnswerNameCode);
                        jsonObject1.put("name",askAnswerType.getName());
                        jsonObject1.put("arr",new JSONArray());
                        arr.add(jsonObject1);
                    }

                }

                //2
                for(AskAnswerRecordSiteItem askAnswerSiteItem:listSiteItem){
                    String askAnswerNameCode = askAnswerSiteItem.getAskAnswerNameCode();
                    JSONObject jsonObject1 = getName(arr,askAnswerNameCode);
                    JSONArray arr1 = jsonObject1.getJSONArray("arr");

                    String askAnswerGroupCode = askAnswerSiteItem.getAskAnswerGroupCode();
                    AskAnswerGroup askAnswerGroup = askAnswerGroupService.getByAskAnswerGroupCode(askAnswerGroupCode);
                    if(!hasGroup(arr1,askAnswerGroupCode)){
                        JSONObject jsonObject2 = new JSONObject();
                        jsonObject2.put("key","group");
                        jsonObject2.put("askAnswerGroupCode",askAnswerGroupCode);
                        jsonObject2.put("name",askAnswerGroup.getName());
                        jsonObject2.put("arr",new JSONArray());
                        arr1.add(jsonObject2);
                    }

                }

                //3
                for(AskAnswerRecordSiteItem askAnswerSiteItem:listSiteItem){
                    String askAnswerNameCode = askAnswerSiteItem.getAskAnswerNameCode();
                    JSONObject jsonObject1 = getName(arr,askAnswerNameCode);
                    JSONArray arr1 = jsonObject1.getJSONArray("arr");

                    String askAnswerGroupCode = askAnswerSiteItem.getAskAnswerGroupCode();
                    JSONObject jsonObject11 = getGroup(arr1,askAnswerGroupCode);
                    JSONArray arr11 = jsonObject11.getJSONArray("arr");

                    String askAnswerItemName = askAnswerSiteItem.getAskAnswerItemName();
                    if(!hasItem(arr11,askAnswerItemName)){
                        JSONObject jsonObject2 = new JSONObject();
                        jsonObject2.put("key","item");
                        jsonObject2.put("askAnswerItemName",askAnswerItemName);
                        jsonObject2.put("name",askAnswerItemName);
                        jsonObject2.put("count",0);
                        arr11.add(jsonObject2);
                    }

                }


                //4
                for(AskAnswerRecordSiteItem askAnswerSiteItem:listSiteItem){

                    String askAnswerNameCode = askAnswerSiteItem.getAskAnswerNameCode();
                    JSONObject jsonObject1 = getName(arr,askAnswerNameCode);
                    JSONArray arr1 = jsonObject1.getJSONArray("arr");

                    String askAnswerGroupCode = askAnswerSiteItem.getAskAnswerGroupCode();
                    JSONObject jsonObject11 = getGroup(arr1,askAnswerGroupCode);
                    JSONArray arr11 = jsonObject11.getJSONArray("arr");

                    String askAnswerItemName = askAnswerSiteItem.getAskAnswerItemName();
                    JSONObject jsonObject111 = getItem(arr11,askAnswerItemName);
                    jsonObject111.put("count",jsonObject111.getIntValue("count")+1);

                }



            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("arr",arr);

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AskAnswerSite/Total", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }






    private boolean hasName(JSONArray arr,String askAnswerNameCode){
        for(int i=0;i<arr.size();i++){
            JSONObject jsonObject1 =arr.getJSONObject(i);
            if(jsonObject1.getString("askAnswerNameCode").equals(askAnswerNameCode)){
                return true;
            }
        }
        return false;
    }
    private JSONObject getName(JSONArray arr,String askAnswerNameCode){
        for(int i=0;i<arr.size();i++){
            JSONObject jsonObject1 =arr.getJSONObject(i);
            if(jsonObject1.getString("askAnswerNameCode").equals(askAnswerNameCode)){
                return jsonObject1;
            }
        }
        return null;
    }


    private boolean hasGroup(JSONArray arr,String askAnswerGroupCode){
        for(int i=0;i<arr.size();i++){
            JSONObject jsonObject1 =arr.getJSONObject(i);
            if(jsonObject1.getString("askAnswerGroupCode").equals(askAnswerGroupCode)){
                return true;
            }
        }
        return false;
    }
    private JSONObject getGroup(JSONArray arr,String askAnswerGroupCode){
        for(int i=0;i<arr.size();i++){
            JSONObject jsonObject1 =arr.getJSONObject(i);
            if(jsonObject1.getString("askAnswerGroupCode").equals(askAnswerGroupCode)){
                return jsonObject1;
            }
        }
        return null;
    }


    private boolean hasItem(JSONArray arr,String askAnswerItemName){
        for(int i=0;i<arr.size();i++){
            JSONObject jsonObject1 =arr.getJSONObject(i);
            if(jsonObject1.getString("askAnswerItemName").equals(askAnswerItemName)){
                return true;
            }
        }
        return false;
    }
    private JSONObject getItem(JSONArray arr,String askAnswerItemName){
        for(int i=0;i<arr.size();i++){
            JSONObject jsonObject1 =arr.getJSONObject(i);
            if(jsonObject1.getString("askAnswerItemName").equals(askAnswerItemName)){
                return jsonObject1;
            }
        }
        return null;
    }





}
